Skip to content

Conversation

@skewb1k
Copy link

@skewb1k skewb1k commented Aug 21, 2025

Adds cjson.encode_indent(indent) option.

If non-empty string provided, JSON values encoded by cjson.encode() will be formatted in a human-readable way, using indent for indentation at each nesting level. Also enables newlines and a space after colons. Must contain only spaces, tabs, line feeds, or carriage returns.

Example:

local cjson = require "cjson"

cjson.encode_indent("  ")
print(cjson.encode({ a = 1, b = { c = 2 } }))
-- {
--   "a": 1,
--   "b": {
--     "c": 2
--   }
-- }

@skewb1k skewb1k force-pushed the feat/pretty-encoding branch 5 times, most recently from a40e320 to 912817c Compare August 25, 2025 19:12
skewb1k added a commit to skewb1k/neovim that referenced this pull request Aug 25, 2025
Problem:
There is no straightforward way to pretty-print objects as JSON.
The existing `vim.inspect` outputs LON.

Solution:
Introduce `pretty` option for `vim.json.encode()`, which enables
pretty-formatting when table provided or `true`. Supports the following keys:

- indent: (integer|string) (default 2) Controls how nested levels are indented.
  - If a integer: indents that many spaces per nesting level.
  - If a string: that string is used for the indentation.
- item_separator: (string) (default "\n") Separator inserted between array elements and object members.
- key_separator: (string) (default " ") Separator inserted after the colon between an object key and its value.

Adapts PR to upstream: openresty/lua-cjson#114.
skewb1k added a commit to skewb1k/neovim that referenced this pull request Aug 25, 2025
Problem:
There is no straightforward way to pretty-print objects as JSON.
The existing `vim.inspect` outputs LON.

Solution:
Introduce `pretty` option for `vim.json.encode()`, which enables
pretty-formatting when table provided or `true`. Supports the following keys:

- indent: (integer|string) (default 2) Controls how nested levels are indented.
  - If a integer: indents that many spaces per nesting level.
  - If a string: that string is used for the indentation.
- item_separator: (string) (default "\n") Separator inserted between array elements and object members.
- key_separator: (string) (default " ") Separator inserted after the colon between an object key and its value.

Adapts PR to upstream: openresty/lua-cjson#114.
skewb1k added a commit to skewb1k/neovim that referenced this pull request Aug 25, 2025
Problem:
There is no straightforward way to pretty-print objects as JSON.
The existing `vim.inspect` outputs LON.

Solution:
Introduce `pretty` option for `vim.json.encode()`, which enables
pretty-formatting when table provided or `true`. Supports the following keys:

- indent: (integer|string) (default 2) Controls how nested levels are indented.
  - If a integer: indents that many spaces per nesting level.
  - If a string: that string is used for the indentation.
- item_separator: (string) (default "\n") Separator inserted between array elements and object members.
- key_separator: (string) (default " ") Separator inserted after the colon between an object key and its value.

Adapts PR to upstream: openresty/lua-cjson#114.
skewb1k added a commit to skewb1k/neovim that referenced this pull request Aug 26, 2025
Problem:
There is no straightforward way to pretty-print objects as JSON.
The existing `vim.inspect` outputs LON.

Solution:
Introduce `pretty` option for `vim.json.encode()`, which enables
pretty-formatting when table non-nil or `true`.

Adapts PR to upstream: openresty/lua-cjson#114.
@skewb1k skewb1k force-pushed the feat/pretty-encoding branch from 912817c to 79f5497 Compare August 26, 2025 22:45
@skewb1k skewb1k changed the title feature: add option for pretty encoding feature: add option to indent encoded output Aug 26, 2025
skewb1k added a commit to skewb1k/neovim that referenced this pull request Aug 26, 2025
Problem:
There is no straightforward way to pretty-print objects as JSON.
The existing `vim.inspect` outputs LON.

Solution:
Introduce an `indent` option for `vim.json.encode()` which enables
human-readable output with configurable indentation.

Adapts PR to upstream: openresty/lua-cjson#114.
@justinmk
Copy link

@zhuizhuhaomeng does this look like a change that openresty/lua-cjson would be open to? Should we send this change to https://github.com/mpx/lua-cjson , does openresty/lua-cjson still sync from there ?

@zhuizhuhaomeng
Copy link

Sure.
This PR will be accepted.

@skewb1k skewb1k force-pushed the feat/pretty-encoding branch from 79f5497 to 862b6c5 Compare August 28, 2025 11:28
@skewb1k
Copy link
Author

skewb1k commented Aug 28, 2025

I noticed that some test names were not unique. I believe it's now ready for review.

@skewb1k skewb1k force-pushed the feat/pretty-encoding branch from 862b6c5 to 1b17610 Compare September 4, 2025 13:15
skewb1k added a commit to skewb1k/neovim that referenced this pull request Sep 4, 2025
Problem:
There is no straightforward way to pretty-print objects as JSON.
The existing `vim.inspect` outputs LON.

Solution:
Introduce an `indent` option for `vim.json.encode()` which enables
human-readable output with configurable indentation.

Adapts PR to upstream: openresty/lua-cjson#114.
justinmk pushed a commit to neovim/neovim that referenced this pull request Sep 7, 2025
Problem:
There is no straightforward way to pretty-print objects as JSON.
The existing `vim.inspect` outputs LON.

Solution:
Introduce an `indent` option for `vim.json.encode()` which enables
human-readable output with configurable indentation.

Adapts PR to upstream: openresty/lua-cjson#114
dundargoc pushed a commit to dundargoc/neovim that referenced this pull request Sep 27, 2025
Problem:
There is no straightforward way to pretty-print objects as JSON.
The existing `vim.inspect` outputs LON.

Solution:
Introduce an `indent` option for `vim.json.encode()` which enables
human-readable output with configurable indentation.

Adapts PR to upstream: openresty/lua-cjson#114
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants